package com.wushi.lenovo.asia5b.app_ui.other.dialog;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mvp.base.BaseDialog;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_model.good.OrderParcelResModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class InternationalNoticeDialog extends BaseDialog {
    @BindView(R.id.tv_deliverynotice_empty)
    TextView tv_deliverynotice_empty;
    @BindView(R.id.tv_delivery_company)
    TextView tv_delivery_company;
    @BindView(R.id.rv_deliverynotice)
    RecyclerView mRecyclerView;
    private DeliverynoticeAdapter mAdapter;
    private final List<OrderParcelResModel.DataBean> mDatas = new ArrayList<>();


    public InternationalNoticeDialog(Context context) {
        super(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_international_notice;
    }

    public void setOrderParcelResModel(OrderParcelResModel orderParcelResModel) {
        tv_delivery_company.setVisibility(View.GONE);
        if (orderParcelResModel != null) {
            if (TextUtils.isEmpty(orderParcelResModel.getCompany())) {
                tv_delivery_company.setVisibility(View.GONE);
            } else {
                tv_delivery_company.setVisibility(View.VISIBLE);
                tv_delivery_company.setText(orderParcelResModel.getCompany());
            }
            setDatas(orderParcelResModel.getData());
        } else {
            setDatas(null);
        }
    }

    private void setDatas(List<OrderParcelResModel.DataBean> list) {
        if (list != null) {
            mDatas.clear();
            mDatas.addAll(list);
            mAdapter.notifyDataSetChanged();
        }
//        if (GlobalValue.isDebug) {
//            mDatas.clear();
//            for (int i = 0; i < 1; i++) {
//                OrderParcelResModel.DataBean bean = new OrderParcelResModel.DataBean();
//                bean.setTime("2018-09-01 18:51:41");
//                bean.setContext("已签收");
//                mDatas.add(bean);
//                mAdapter.notifyDataSetChanged();
//            }
//        }
        tv_deliverynotice_empty.setVisibility(mDatas.size() == 0 ? View.VISIBLE : View.GONE);
        mRecyclerView.setVisibility(mDatas.size() != 0 ? View.VISIBLE : View.GONE);
    }

    @Override
    protected void initView() {
        tv_deliverynotice_empty.setVisibility(View.GONE);
        mRecyclerView.setVisibility(View.GONE);
        mAdapter = new DeliverynoticeAdapter(mDatas);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mAdapter);
    }


    @Override
    protected void initListener() {

    }

    @OnClick({R.id.bt_dialog_ok, R.id.iv_dialog_close})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_dialog_ok:
            case R.id.iv_dialog_close:
                dismiss();
                break;
            default:
                break;
        }
    }

    private static class DeliverynoticeAdapter extends BaseQuickAdapter<OrderParcelResModel.DataBean, BaseViewHolder> {

        public DeliverynoticeAdapter(@Nullable List<OrderParcelResModel.DataBean> data) {
            super(R.layout.item_international2_adapter, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, OrderParcelResModel.DataBean item) {
            TextView tv_deliverynotice_data = helper.getView(R.id.tv_deliverynotice_data);
            TextView tv_deliverynotice_time = helper.getView(R.id.tv_deliverynotice_time);
            TextView tv_deliverynotice_context = helper.getView(R.id.tv_deliverynotice_context);
            LinearLayout ll_delivery_cicle_vessel = helper.getView(R.id.ll_delivery_cicle_vessel);

            tv_deliverynotice_time.setText(item.getTime());
            tv_deliverynotice_context.setText(item.getContext());
            if (TextUtils.isEmpty(item.getDate())) {
                ll_delivery_cicle_vessel.setVisibility(View.GONE);
            } else {
                ll_delivery_cicle_vessel.setVisibility(View.VISIBLE);
                tv_deliverynotice_data.setText(item.getDate());
            }
        }
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getWidthStyle() {
        return getScreenWidth(0.9f);
    }
}
